/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.coders.tests;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;

import org.junit.Test;
import org.latestbit.smartobjects.coders.IDataTypesCoder;
import org.latestbit.smartobjects.coders.StdDataTypesCoder;
import org.latestbit.smartobjects.io.ResizableByteBuffer;
import org.latestbit.smartobjects.testmodel.*;

import static org.junit.Assert.*;


public class StdDataTypesCoderTest {
	
	@Test
	public void testStdDataTypesCoder() throws Exception {
		IDataTypesCoder coder = new StdDataTypesCoder();
		ResizableByteBuffer rszBuffer = new ResizableByteBuffer(true, 100, 1024, 100);
		
		coder.encodeString( "Hello", rszBuffer );
		coder.encodeWString( "HelloW", rszBuffer );
		coder.encodeInt16( (short)0x0A0B, rszBuffer );
		coder.encodeInt32( 0x0C0D0E0F, rszBuffer );
		coder.encodeInt64( 0x0C0D0E0F01020304L, rszBuffer );
		coder.encodeFloatingLength(32, rszBuffer);
		coder.encodeFloatingLength(190, rszBuffer);
		coder.encodeFloatingLength(0xFFAA, rszBuffer);
		coder.encodeFloatingLength(0xFFFF0, rszBuffer);
		coder.encodeFloatingLength(0xFFFFFF, rszBuffer);
		coder.encodeBoolean( true, rszBuffer );
		coder.encodeBoolean( false, rszBuffer );
		coder.encodeBooleanArray( new boolean[] {true, false, true}, rszBuffer );
		coder.encodeByte( ( byte)0x0A, rszBuffer);
		coder.encodeByteArray( new byte[] { 0x0A, 0x0B, 0x0C }, rszBuffer );
		coder.encodeFloat(0.5f, rszBuffer);
		coder.encodeDouble(0.5d, rszBuffer);
		coder.encodeBigInteger(new BigInteger("100"), rszBuffer);
		coder.encodeBigDecimal(new BigDecimal("100.05"), rszBuffer);
		coder.encodeStructureDataTypeBegin(rszBuffer);
		coder.encodeStructureDataTypeEnd(rszBuffer);
		coder.encodeNull(rszBuffer);
		int beginPos = coder.encodeChoiceDataTypeBegin( rszBuffer );
		coder.encodeChoiceSelectedField("test", rszBuffer );
		coder.encodeChoiceDataTypeEnd( beginPos, rszBuffer );
		
		coder.encodeInt16Array( new short[] { 0x0A, 0x0B, 0x0C }, rszBuffer );
		coder.encodeInt32Array( new int[] { 0x0A, 0x0B, 0x0C }, rszBuffer );
		coder.encodeInt64Array( new long[] { 0x0A, 0x0B, 0x0C }, rszBuffer );
		coder.encodeUInt16Array( new short[] { 0x0A, 0x0B, 0x0C }, rszBuffer );
		coder.encodeUInt32Array( new int[] { 0x0A, 0x0B, 0x0C }, rszBuffer );
		coder.encodeUInt64Array( new long[] { 0x0A, 0x0B, 0x0C }, rszBuffer );
		coder.encodeStringArray( new String[] { "Hello", "Hello2" } , rszBuffer );
		coder.encodeWStringArray( new String[] { "Hello", "Hello2" } , rszBuffer );
		coder.encodeChar('A', rszBuffer);
		coder.encodeWChar('\u0401', rszBuffer);

		coder.encodeString( null, rszBuffer);
		coder.encodeWString( null, rszBuffer);
		coder.encodeInt16Array( null, rszBuffer );
		coder.encodeInt32Array( null, rszBuffer );
		coder.encodeInt64Array( null, rszBuffer );
		coder.encodeUInt16Array( null, rszBuffer );
		coder.encodeUInt32Array( null, rszBuffer );
		coder.encodeUInt64Array( null, rszBuffer );
				
		ByteBuffer stdBuffer = rszBuffer.getBuffer();
		stdBuffer.flip();
		
		assertEquals( "Hello", coder.decodeString(stdBuffer) );
		assertEquals( "HelloW", coder.decodeWString(stdBuffer) );
		assertEquals( (short)0x0A0B, coder.decodeInt16(stdBuffer) );
		assertEquals( 0x0C0D0E0F, coder.decodeInt32(stdBuffer) );
		assertEquals( 0x0C0D0E0F01020304L, coder.decodeInt64(stdBuffer) );
		assertEquals( 32, coder.decodeFloatingLength(stdBuffer) );
		assertEquals( 190, coder.decodeFloatingLength(stdBuffer) );
		assertEquals( 0xFFAA, coder.decodeFloatingLength(stdBuffer) );
		assertEquals( 0xFFFF0, coder.decodeFloatingLength(stdBuffer) );
		assertEquals( 0xFFFFFF, coder.decodeFloatingLength(stdBuffer) );
		assertTrue ( coder.decodeBoolean(stdBuffer) );
		assertFalse ( coder.decodeBoolean(stdBuffer) );
		boolean[] bArray = coder.decodeBooleanArray(stdBuffer);
		assertTrue ( bArray[0] == true && bArray[1] == false && bArray[2] == true );
		assertEquals( (short)0x0A, coder.decodeByte(stdBuffer) );
		assertArrayEquals ( new byte[] { 0x0A, 0x0B, 0x0C }, coder.decodeByteArray(stdBuffer) );
		assertEquals( 0.5f, coder.decodeFloat(stdBuffer), 0.1 );
		assertEquals( 0.5d, coder.decodeDouble(stdBuffer), 0.1 );		
		assertTrue ( coder.decodeBigInteger( stdBuffer ).equals(new BigInteger("100")) );
		assertTrue ( coder.decodeBigDecimal( stdBuffer ).equals(new BigDecimal("100.05")) );
		
		assertTrue ( coder.decodeStructureDataTypeBegin(stdBuffer) );
		coder.decodeStructureDataTypeEnd(stdBuffer);
		assertFalse ( coder.decodeStructureDataTypeBegin(stdBuffer) );
		beginPos = coder.decodeChoiceDataTypeBegin(stdBuffer);
		assertTrue ( beginPos > 0 );
		assertEquals ( "test", coder.decodeChoiceSelectedField(stdBuffer));
		coder.decodeChoiceDataTypeEnd(beginPos, stdBuffer);
		assertArrayEquals ( new short[] { 0x0A, 0x0B, 0x0C }, coder.decodeInt16Array(stdBuffer) );
		assertArrayEquals ( new int[] { 0x0A, 0x0B, 0x0C }, coder.decodeInt32Array(stdBuffer) );
		assertArrayEquals ( new long[] { 0x0A, 0x0B, 0x0C }, coder.decodeInt64Array(stdBuffer) );
		assertArrayEquals ( new short[] { 0x0A, 0x0B, 0x0C }, coder.decodeUInt16Array(stdBuffer) );
		assertArrayEquals ( new int[] { 0x0A, 0x0B, 0x0C }, coder.decodeUInt32Array(stdBuffer) );
		assertArrayEquals ( new long[] { 0x0A, 0x0B, 0x0C }, coder.decodeUInt64Array(stdBuffer) );
		assertArrayEquals ( new String[] { "Hello", "Hello2" }, coder.decodeStringArray(stdBuffer) );
		assertArrayEquals ( new String[] { "Hello", "Hello2" }, coder.decodeWStringArray(stdBuffer) );
		assertEquals ( 'A', coder.decodeChar(stdBuffer) );
		assertEquals ( '\u0401', coder.decodeWChar(stdBuffer) );
		assertNull ( coder.decodeString(stdBuffer) );		
		assertNull ( coder.decodeWString(stdBuffer) );
		assertNull ( coder.decodeInt16Array(stdBuffer) );
		assertNull ( coder.decodeInt32Array(stdBuffer) );
		assertNull ( coder.decodeInt64Array(stdBuffer) );
		assertNull ( coder.decodeUInt16Array(stdBuffer) );
		assertNull ( coder.decodeUInt32Array(stdBuffer) );
		assertNull ( coder.decodeUInt64Array(stdBuffer) );
		
	}
	
	@Test
	public void testStdDataTypesCoderNamedParts() throws Exception {

		IDataTypesCoder coder = new StdDataTypesCoder();
		ResizableByteBuffer rszBuffer = new ResizableByteBuffer(true, 100, 1024, 100);
		byte[] itemTestBuffer =  new byte[] { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 };
		
		coder.encodeStructureDataTypeBegin(rszBuffer);
		int beginPos = coder.encodeNamedDataPartBegin("testpart1", rszBuffer);
		rszBuffer.put( itemTestBuffer );
		coder.encodeNamedDataPartEnd( beginPos, rszBuffer);
		
		beginPos = coder.encodeNamedDataPartBegin("testpart2", rszBuffer);
		rszBuffer.put( itemTestBuffer );
		coder.encodeNamedDataPartEnd( beginPos, rszBuffer);

		beginPos = coder.encodeNamedDataPartBegin("testpart3", rszBuffer);
		rszBuffer.put( itemTestBuffer );
		coder.encodeNamedDataPartEnd( beginPos, rszBuffer);
		
		coder.encodeStructureDataTypeEnd(rszBuffer);

		ByteBuffer stdBuffer = rszBuffer.getBuffer();
		stdBuffer.flip();
		
		assertTrue ( coder.decodeStructureDataTypeBegin(stdBuffer) );
		
		beginPos = coder.decodeNamedDataPartBegin("testpart2", stdBuffer );
		coder.decodeNamedDataPartEnd(beginPos, stdBuffer);

		beginPos = coder.decodeNamedDataPartBegin("testpart3", stdBuffer );
		byte[] rcvdTestBuffer = new byte[itemTestBuffer.length];
		rszBuffer.getBuffer().get(rcvdTestBuffer);
		assertArrayEquals(itemTestBuffer,rcvdTestBuffer);
		coder.decodeNamedDataPartEnd(beginPos, stdBuffer);
		
		coder.decodeStructureDataTypeEnd(stdBuffer);
	}
	
	@Test
	public void testCoderForGeneratedObjects() throws Exception {
		IDataTypesCoder coder = new StdDataTypesCoder();
		ResizableByteBuffer rszBuffer = new ResizableByteBuffer(true, 100, 1024, 100);
		
		SimpleStructure structureToEncode = new SimpleStructure();
		structureToEncode.setIntVal(BigInteger.TEN);
		structureToEncode.setStrVal("Hello");
		structureToEncode.serialize(coder, null, rszBuffer);
		
		SimpleException exceptionToEncode = new SimpleException();
		exceptionToEncode.setMyCode ( (short) 0xAA );
		exceptionToEncode.serialize ( coder, null, rszBuffer );
		
		SimpleChoice choiceToEncode = new SimpleChoice();
		choiceToEncode.setStrVal("Hello");
		choiceToEncode.serialize ( coder, null, rszBuffer );

		SimpleChildStructure childStructureToEncode = new SimpleChildStructure();
		childStructureToEncode.setIntVal(BigInteger.TEN);
		childStructureToEncode.setStrVal("Hello");
		childStructureToEncode.setStrChildVal("Hello2");
		childStructureToEncode.serialize(coder, null, rszBuffer);
		childStructureToEncode.serialize(coder, null, rszBuffer);
		
		ByteBuffer stdBuffer = rszBuffer.getBuffer();
		stdBuffer.flip();
		
		SimpleStructure decodedStructure = new SimpleStructure();
		decodedStructure.deserialize(coder, null, stdBuffer );		
		assertEquals ( structureToEncode.getIntVal(), decodedStructure.getIntVal() );
		assertEquals ( structureToEncode.getStrVal(), decodedStructure.getStrVal() );
		
		SimpleException decodedException = new SimpleException();
		decodedException.deserialize(coder, null, stdBuffer );
		assertEquals( exceptionToEncode.getMyCode(), decodedException.getMyCode() );
		assertEquals( exceptionToEncode.getMessage(), decodedException.getMessage() );
		
		SimpleChoice decodedChoice = new SimpleChoice();
		decodedChoice.deserialize(coder, null, stdBuffer);
		assertEquals( decodedChoice.isIntValSelected(), decodedChoice.isIntValSelected() );
		assertEquals( decodedChoice.isStrValSelected(), decodedChoice.isStrValSelected() );
		assertEquals( decodedChoice.getStrVal(), decodedChoice.getStrVal() );
		
		SimpleChildStructure decodedChildStructure = new SimpleChildStructure();
		decodedChildStructure.deserialize(coder, null, stdBuffer );
		assertEquals ( childStructureToEncode.getIntVal(), decodedChildStructure.getIntVal() );
		assertEquals ( childStructureToEncode.getStrVal(), decodedChildStructure.getStrVal() );
		assertEquals ( childStructureToEncode.getStrChildVal(), decodedChildStructure.getStrChildVal() );
		
		decodedStructure = new SimpleStructure();
		decodedStructure.deserialize(coder, null, stdBuffer );		
		assertEquals ( structureToEncode.getIntVal(), decodedStructure.getIntVal() );
		assertEquals ( structureToEncode.getStrVal(), decodedStructure.getStrVal() );		
	}
}
